/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TelaPrincipal.java
 *
 * Created on 19/09/2011, 01:08:51
 */
package visao;

import controlador.AlvyRay;
import controlador.Controle;
import controlador.Luz;
import controlador.Vetor;
import controlador.ZBuffer;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.HashSet;
import modelo.objeto3d.Face3d;
import modelo.objeto3d.Objeto3d;
import modelo.objeto3d.Ponto3d;

/**
 *
 * @author TyTu
 */
public class TelaPrincipal extends javax.swing.JFrame {

    /** Creates new form TelaPrincipal */
    public TelaPrincipal() {
        initComponents();
    }
    private Point p = new Point();
    private ZBuffer zBuffer;

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        buttonGroup3 = new javax.swing.ButtonGroup();
        jToolBar1 = new javax.swing.JToolBar();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        jRadioButtonMover = new javax.swing.JRadioButton();
        jRadioButtonEscala = new javax.swing.JRadioButton();
        jRadioButtonRotacionar = new javax.swing.JRadioButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jRadioButtonComOcultacao = new javax.swing.JRadioButton();
        jRadioButtonSemOcultacao = new javax.swing.JRadioButton();
        jSeparator11 = new javax.swing.JToolBar.Separator();
        jRadioButtonComPreenchimento = new javax.swing.JRadioButton();
        jRadioButtonSemPreenchimento = new javax.swing.JRadioButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        jComboBoxObjetos = new javax.swing.JComboBox();
        jButtonDelete = new javax.swing.JButton();
        jSeparator8 = new javax.swing.JToolBar.Separator();
        jButtonLimpar = new javax.swing.JButton();
        jSeparator7 = new javax.swing.JToolBar.Separator();
        jButtonRenderizar = new javax.swing.JButton();
        jTabbedPaneCena = new javax.swing.JTabbedPane();
        jPanelTodos = new javax.swing.JPanel();
        jPanelTodosXY = new javax.swing.JPanel();
        jPanelTodosYZ = new javax.swing.JPanel();
        jPanelTodosXZ = new javax.swing.JPanel();
        jPanelPerpectiva = new javax.swing.JPanel();
        jPanelXY = new javax.swing.JPanel();
        jPanelXZ = new javax.swing.JPanel();
        jPanelYZ = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItemNovoObjeto = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItemEditarCena = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jToolBar1.setRollover(true);
        jToolBar1.add(jSeparator3);

        buttonGroup1.add(jRadioButtonMover);
        jRadioButtonMover.setSelected(true);
        jRadioButtonMover.setText("Mover");
        jRadioButtonMover.setEnabled(false);
        jRadioButtonMover.setFocusable(false);
        jRadioButtonMover.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jRadioButtonMover.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jRadioButtonMover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonMoverActionPerformed(evt);
            }
        });
        jToolBar1.add(jRadioButtonMover);

        buttonGroup1.add(jRadioButtonEscala);
        jRadioButtonEscala.setText("Escala");
        jRadioButtonEscala.setEnabled(false);
        jRadioButtonEscala.setFocusable(false);
        jRadioButtonEscala.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jRadioButtonEscala.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jRadioButtonEscala.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonEscalaActionPerformed(evt);
            }
        });
        jToolBar1.add(jRadioButtonEscala);

        buttonGroup1.add(jRadioButtonRotacionar);
        jRadioButtonRotacionar.setText("Rotacionar");
        jRadioButtonRotacionar.setEnabled(false);
        jRadioButtonRotacionar.setFocusable(false);
        jRadioButtonRotacionar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jRadioButtonRotacionar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jRadioButtonRotacionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonRotacionarActionPerformed(evt);
            }
        });
        jToolBar1.add(jRadioButtonRotacionar);
        jToolBar1.add(jSeparator1);

        buttonGroup2.add(jRadioButtonComOcultacao);
        jRadioButtonComOcultacao.setText("Com Eliminação de Faces");
        jRadioButtonComOcultacao.setEnabled(false);
        jRadioButtonComOcultacao.setFocusable(false);
        jRadioButtonComOcultacao.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jRadioButtonComOcultacao.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jRadioButtonComOcultacao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonComOcultacaoActionPerformed(evt);
            }
        });
        jToolBar1.add(jRadioButtonComOcultacao);

        buttonGroup2.add(jRadioButtonSemOcultacao);
        jRadioButtonSemOcultacao.setText("Sem Eliminação de Faces");
        jRadioButtonSemOcultacao.setEnabled(false);
        jRadioButtonSemOcultacao.setFocusable(false);
        jRadioButtonSemOcultacao.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jRadioButtonSemOcultacao.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jRadioButtonSemOcultacao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonSemOcultacaoActionPerformed(evt);
            }
        });
        jToolBar1.add(jRadioButtonSemOcultacao);
        jToolBar1.add(jSeparator11);

        buttonGroup3.add(jRadioButtonComPreenchimento);
        jRadioButtonComPreenchimento.setText("Com Preenchimento");
        jRadioButtonComPreenchimento.setFocusable(false);
        jRadioButtonComPreenchimento.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jRadioButtonComPreenchimento.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jRadioButtonComPreenchimento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonComPreenchimentoActionPerformed(evt);
            }
        });
        jToolBar1.add(jRadioButtonComPreenchimento);

        buttonGroup3.add(jRadioButtonSemPreenchimento);
        jRadioButtonSemPreenchimento.setSelected(true);
        jRadioButtonSemPreenchimento.setText("Sem Preenchimento");
        jRadioButtonSemPreenchimento.setFocusable(false);
        jRadioButtonSemPreenchimento.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jRadioButtonSemPreenchimento.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jRadioButtonSemPreenchimento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonSemPreenchimentoActionPerformed(evt);
            }
        });
        jToolBar1.add(jRadioButtonSemPreenchimento);
        jToolBar1.add(jSeparator2);

        jComboBoxObjetos.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBoxObjetos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxObjetosActionPerformed(evt);
            }
        });
        jToolBar1.add(jComboBoxObjetos);
        jComboBoxObjetos.removeAllItems();

        jButtonDelete.setText("Deletar");
        jButtonDelete.setFocusable(false);
        jButtonDelete.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonDelete.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeleteActionPerformed(evt);
            }
        });
        jToolBar1.add(jButtonDelete);
        jToolBar1.add(jSeparator8);

        jButtonLimpar.setText("Limpar Cena");
        jButtonLimpar.setFocusable(false);
        jButtonLimpar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonLimpar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLimparActionPerformed(evt);
            }
        });
        jToolBar1.add(jButtonLimpar);
        jToolBar1.add(jSeparator7);

        jButtonRenderizar.setText("Renderizar");
        jButtonRenderizar.setFocusable(false);
        jButtonRenderizar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonRenderizar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonRenderizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRenderizarActionPerformed(evt);
            }
        });
        jToolBar1.add(jButtonRenderizar);

        jTabbedPaneCena.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTabbedPaneCenaMouseClicked(evt);
            }
        });

        jPanelTodos.setLayout(new java.awt.GridLayout(2, 2, 2, 2));

        jPanelTodosXY.setBackground(new java.awt.Color(255, 255, 255));
        jPanelTodosXY.setMinimumSize(new java.awt.Dimension(0, 0));
        jPanelTodosXY.setPreferredSize(new java.awt.Dimension(400, 300));
        jPanelTodosXY.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jPanelTodosXYMousePressed(evt);
            }
        });
        jPanelTodosXY.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jPanelTodosXYMouseDragged(evt);
            }
        });

        javax.swing.GroupLayout jPanelTodosXYLayout = new javax.swing.GroupLayout(jPanelTodosXY);
        jPanelTodosXY.setLayout(jPanelTodosXYLayout);
        jPanelTodosXYLayout.setHorizontalGroup(
            jPanelTodosXYLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 507, Short.MAX_VALUE)
        );
        jPanelTodosXYLayout.setVerticalGroup(
            jPanelTodosXYLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 277, Short.MAX_VALUE)
        );

        jPanelTodos.add(jPanelTodosXY);

        jPanelTodosYZ.setBackground(new java.awt.Color(255, 255, 255));
        jPanelTodosYZ.setMinimumSize(new java.awt.Dimension(0, 0));
        jPanelTodosYZ.setPreferredSize(new java.awt.Dimension(400, 300));
        jPanelTodosYZ.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jPanelTodosYZMousePressed(evt);
            }
        });
        jPanelTodosYZ.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jPanelTodosYZMouseDragged(evt);
            }
        });

        javax.swing.GroupLayout jPanelTodosYZLayout = new javax.swing.GroupLayout(jPanelTodosYZ);
        jPanelTodosYZ.setLayout(jPanelTodosYZLayout);
        jPanelTodosYZLayout.setHorizontalGroup(
            jPanelTodosYZLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 507, Short.MAX_VALUE)
        );
        jPanelTodosYZLayout.setVerticalGroup(
            jPanelTodosYZLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 277, Short.MAX_VALUE)
        );

        jPanelTodos.add(jPanelTodosYZ);

        jPanelTodosXZ.setBackground(new java.awt.Color(255, 255, 255));
        jPanelTodosXZ.setMinimumSize(new java.awt.Dimension(0, 0));
        jPanelTodosXZ.setPreferredSize(new java.awt.Dimension(400, 300));
        jPanelTodosXZ.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jPanelTodosXZMousePressed(evt);
            }
        });
        jPanelTodosXZ.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jPanelTodosXZMouseDragged(evt);
            }
        });

        javax.swing.GroupLayout jPanelTodosXZLayout = new javax.swing.GroupLayout(jPanelTodosXZ);
        jPanelTodosXZ.setLayout(jPanelTodosXZLayout);
        jPanelTodosXZLayout.setHorizontalGroup(
            jPanelTodosXZLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 507, Short.MAX_VALUE)
        );
        jPanelTodosXZLayout.setVerticalGroup(
            jPanelTodosXZLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 277, Short.MAX_VALUE)
        );

        jPanelTodos.add(jPanelTodosXZ);

        jPanelPerpectiva.setBackground(new java.awt.Color(255, 255, 255));
        jPanelPerpectiva.setMinimumSize(new java.awt.Dimension(0, 0));
        jPanelPerpectiva.setPreferredSize(new java.awt.Dimension(400, 300));
        jPanelPerpectiva.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jPanelPerpectivaMousePressed(evt);
            }
        });

        javax.swing.GroupLayout jPanelPerpectivaLayout = new javax.swing.GroupLayout(jPanelPerpectiva);
        jPanelPerpectiva.setLayout(jPanelPerpectivaLayout);
        jPanelPerpectivaLayout.setHorizontalGroup(
            jPanelPerpectivaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 507, Short.MAX_VALUE)
        );
        jPanelPerpectivaLayout.setVerticalGroup(
            jPanelPerpectivaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 277, Short.MAX_VALUE)
        );

        jPanelTodos.add(jPanelPerpectiva);

        jTabbedPaneCena.addTab("Todos", jPanelTodos);

        jPanelXY.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jPanelXYMousePressed(evt);
            }
        });
        jPanelXY.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jPanelXYMouseDragged(evt);
            }
        });

        javax.swing.GroupLayout jPanelXYLayout = new javax.swing.GroupLayout(jPanelXY);
        jPanelXY.setLayout(jPanelXYLayout);
        jPanelXYLayout.setHorizontalGroup(
            jPanelXYLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1017, Short.MAX_VALUE)
        );
        jPanelXYLayout.setVerticalGroup(
            jPanelXYLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 556, Short.MAX_VALUE)
        );

        jTabbedPaneCena.addTab("XY", jPanelXY);

        jPanelXZ.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jPanelXZMousePressed(evt);
            }
        });
        jPanelXZ.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jPanelXZMouseDragged(evt);
            }
        });

        javax.swing.GroupLayout jPanelXZLayout = new javax.swing.GroupLayout(jPanelXZ);
        jPanelXZ.setLayout(jPanelXZLayout);
        jPanelXZLayout.setHorizontalGroup(
            jPanelXZLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1017, Short.MAX_VALUE)
        );
        jPanelXZLayout.setVerticalGroup(
            jPanelXZLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 556, Short.MAX_VALUE)
        );

        jTabbedPaneCena.addTab("XZ", jPanelXZ);

        jPanelYZ.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jPanelYZMousePressed(evt);
            }
        });
        jPanelYZ.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jPanelYZMouseDragged(evt);
            }
        });

        javax.swing.GroupLayout jPanelYZLayout = new javax.swing.GroupLayout(jPanelYZ);
        jPanelYZ.setLayout(jPanelYZLayout);
        jPanelYZLayout.setHorizontalGroup(
            jPanelYZLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1017, Short.MAX_VALUE)
        );
        jPanelYZLayout.setVerticalGroup(
            jPanelYZLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 556, Short.MAX_VALUE)
        );

        jTabbedPaneCena.addTab("YZ", jPanelYZ);

        jMenu1.setText("Arquivo");

        jMenuItemNovoObjeto.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemNovoObjeto.setText("Novo Objeto");
        jMenuItemNovoObjeto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemNovoObjetoActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemNovoObjeto);

        jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem4.setText("GravarCena");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem4);

        jMenuItem5.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem5.setText("BuscarCena");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem5);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Editar");
        jMenu2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu2ActionPerformed(evt);
            }
        });

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("Perspectiva");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuItemEditarCena.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemEditarCena.setText("Cena");
        jMenuItemEditarCena.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemEditarCenaActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItemEditarCena);

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem2.setText("Objeto ");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem2);

        jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem3.setText("Luz");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem3);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 1012, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jTabbedPaneCena, javax.swing.GroupLayout.DEFAULT_SIZE, 1022, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPaneCena, javax.swing.GroupLayout.DEFAULT_SIZE, 584, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItemNovoObjetoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemNovoObjetoActionPerformed
        // TODO add your handling code here:
        Controle.CriarPerfil2d();
    }//GEN-LAST:event_jMenuItemNovoObjetoActionPerformed

    private void jRadioButtonMoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMoverActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jRadioButtonMoverActionPerformed

    private void jRadioButtonEscalaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonEscalaActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jRadioButtonEscalaActionPerformed

    private void jRadioButtonRotacionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonRotacionarActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jRadioButtonRotacionarActionPerformed

    private void jButtonLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLimparActionPerformed
        // TODO add your handling code here:
        controlador.Controle.LimpaCena();
        jRadioButtonComOcultacao.setEnabled(false);
        jRadioButtonEscala.setEnabled(false);
        jRadioButtonMover.setEnabled(false);
        jRadioButtonRotacionar.setEnabled(false);
        jRadioButtonSemOcultacao.setEnabled(false);
        atualiza();


}//GEN-LAST:event_jButtonLimparActionPerformed

    private void jComboBoxObjetosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxObjetosActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBoxObjetosActionPerformed

    private void jRadioButtonComOcultacaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonComOcultacaoActionPerformed
        // TODO add your handling code here:
        desenha();
    }//GEN-LAST:event_jRadioButtonComOcultacaoActionPerformed

    private void jRadioButtonSemOcultacaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonSemOcultacaoActionPerformed
        // TODO add your handling code here:
        atualiza();
    }//GEN-LAST:event_jRadioButtonSemOcultacaoActionPerformed

    private void jPanelTodosXYMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanelTodosXYMousePressed
        // TODO add your handling code here:
        p.x = evt.getX();
        p.y = evt.getY();
    }//GEN-LAST:event_jPanelTodosXYMousePressed

    private void jPanelTodosXZMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanelTodosXZMousePressed
        // TODO add your handling code here:
        p.x = evt.getX();
        p.y = evt.getY();
    }//GEN-LAST:event_jPanelTodosXZMousePressed

    private void jPanelTodosYZMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanelTodosYZMousePressed
        // TODO add your handling code here:
        p.x = evt.getX();
        p.y = evt.getY();
    }//GEN-LAST:event_jPanelTodosYZMousePressed

    private void jPanelPerpectivaMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanelPerpectivaMousePressed
        // TODO add your handling code here:
        p.x = evt.getX();
        p.y = evt.getY();
    }//GEN-LAST:event_jPanelPerpectivaMousePressed

    private void jPanelXYMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanelXYMousePressed
        // TODO add your handling code here:
        p.x = evt.getX();
        p.y = evt.getY();
    }//GEN-LAST:event_jPanelXYMousePressed

    private void jPanelXZMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanelXZMousePressed
        // TODO add your handling code here:
        p.x = evt.getX();
        p.y = evt.getY();
    }//GEN-LAST:event_jPanelXZMousePressed

    private void jPanelYZMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanelYZMousePressed
        // TODO add your handling code here:
        p.x = evt.getX();
        p.y = evt.getY();
    }//GEN-LAST:event_jPanelYZMousePressed

    private void jPanelTodosXYMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanelTodosXYMouseDragged
        // TODO add your handling code here:

        Point p1 = evt.getPoint();
        double dx = (double) p1.x - p.x;
        double dy = (double) p1.y - p.y;
        Objeto3d obj3d = (Objeto3d) jComboBoxObjetos.getSelectedItem();
        //if (controlador.Controle.getPerfil2d().isOnFaces(new Ponto2d(p1.x, p1.y))) {
        if (jRadioButtonMover.isSelected()) {
            obj3d.translacao(dx, dy, 0);


        } else if (jRadioButtonEscala.isSelected()) {
            obj3d.escala((dx / 100) + 1, (dy / 100) + 1, 1);


        } else if (jRadioButtonRotacionar.isSelected()) {
            double angulo = (dx - controlador.Controle.getPerfil2d().getCentro().getX())
                    / (dy - controlador.Controle.getPerfil2d().getCentro().getY());
            obj3d.rotacaoEixoZ(angulo);


        }
        p = p1;
        desenha();
        //}


    }//GEN-LAST:event_jPanelTodosXYMouseDragged

private void jPanelTodosXZMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanelTodosXZMouseDragged
// TODO add your handling code here:

    Point p1 = evt.getPoint();
    double dx = (double) p1.x - p.x;
    double dy = (double) p1.y - p.y;
    Objeto3d obj3d = (Objeto3d) jComboBoxObjetos.getSelectedItem();
    //if (controlador.Controle.getPerfil2d().isOnFaces(new Ponto2d(p1.x, p1.y))) {
    if (jRadioButtonMover.isSelected()) {
        obj3d.translacao(dx, 0, dy);


    } else if (jRadioButtonEscala.isSelected()) {
        obj3d.escala((dx / 100) + 1, 1, (dy / 100) + 1);


    } else if (jRadioButtonRotacionar.isSelected()) {
        double angulo = (dx - controlador.Controle.getPerfil2d().getCentro().getX())
                / (dy - controlador.Controle.getPerfil2d().getCentro().getY());
        obj3d.rotacaoEixoY(angulo);


    }
    p = p1;
    desenha();


}//GEN-LAST:event_jPanelTodosXZMouseDragged

private void jPanelTodosYZMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanelTodosYZMouseDragged
// TODO add your handling code here:
    Point p1 = evt.getPoint();
    double dx = (double) p1.x - p.x;
    double dy = (double) p1.y - p.y;
    Objeto3d obj3d = (Objeto3d) jComboBoxObjetos.getSelectedItem();
    //if (controlador.Controle.getPerfil2d().isOnFaces(new Ponto2d(p1.x, p1.y))) {
    if (jRadioButtonMover.isSelected()) {
        obj3d.translacao(0, dx, dy);


    } else if (jRadioButtonEscala.isSelected()) {
        obj3d.escala(1, (dx / 100) + 1, (dy / 100) + 1);


    } else if (jRadioButtonRotacionar.isSelected()) {
        double angulo = (dx - controlador.Controle.getPerfil2d().getCentro().getX())
                / (dy - controlador.Controle.getPerfil2d().getCentro().getY());
        obj3d.rotacaoEixoX(angulo);


    }
    p = p1;
    desenha();
}//GEN-LAST:event_jPanelTodosYZMouseDragged

private void jTabbedPaneCenaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTabbedPaneCenaMouseClicked
// TODO add your handling code here:
    desenha();
}//GEN-LAST:event_jTabbedPaneCenaMouseClicked

private void jPanelXYMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanelXYMouseDragged
// TODO add your handling code here:
    Point p1 = evt.getPoint();
    double dx = (double) p1.x - p.x;
    double dy = (double) p1.y - p.y;
    Objeto3d obj3d = (Objeto3d) jComboBoxObjetos.getSelectedItem();
    //if (controlador.Controle.getPerfil2d().isOnFaces(new Ponto2d(p1.x, p1.y))) {
    if (jRadioButtonMover.isSelected()) {
        obj3d.translacao(dx, dy, 0);


    } else if (jRadioButtonEscala.isSelected()) {
        obj3d.escala((dx / 100) + 1, (dy / 100) + 1, 1);


    } else if (jRadioButtonRotacionar.isSelected()) {
        double angulo = (dx - controlador.Controle.getPerfil2d().getCentro().getX())
                / (dy - controlador.Controle.getPerfil2d().getCentro().getY());
        obj3d.rotacaoEixoZ(angulo);


    }
    p = p1;
    desenha();
    //}
}//GEN-LAST:event_jPanelXYMouseDragged

private void jPanelXZMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanelXZMouseDragged
// TODO add your handling code here:
    Point p1 = evt.getPoint();
    double dx = (double) p1.x - p.x;
    double dy = (double) p1.y - p.y;
    Objeto3d obj3d = (Objeto3d) jComboBoxObjetos.getSelectedItem();
    //if (controlador.Controle.getPerfil2d().isOnFaces(new Ponto2d(p1.x, p1.y))) {
    if (jRadioButtonMover.isSelected()) {
        obj3d.translacao(dx, 0, dy);


    } else if (jRadioButtonEscala.isSelected()) {
        obj3d.escala((dx / 100) + 1, 1, (dy / 100) + 1);


    } else if (jRadioButtonRotacionar.isSelected()) {
        double angulo = (dx - controlador.Controle.getPerfil2d().getCentro().getX())
                / (dy - controlador.Controle.getPerfil2d().getCentro().getY());
        obj3d.rotacaoEixoY(angulo);


    }
    p = p1;
    desenha();
}//GEN-LAST:event_jPanelXZMouseDragged

private void jPanelYZMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanelYZMouseDragged
// TODO add your handling code here:
    Point p1 = evt.getPoint();
    double dx = (double) p1.x - p.x;
    double dy = (double) p1.y - p.y;
    Objeto3d obj3d = (Objeto3d) jComboBoxObjetos.getSelectedItem();
    //if (controlador.Controle.getPerfil2d().isOnFaces(new Ponto2d(p1.x, p1.y))) {
    if (jRadioButtonMover.isSelected()) {
        obj3d.translacao(0, dx, dy);


    } else if (jRadioButtonEscala.isSelected()) {
        obj3d.escala(1, (dx / 100) + 1, (dy / 100) + 1);


    } else if (jRadioButtonRotacionar.isSelected()) {
        double angulo = (dx - controlador.Controle.getPerfil2d().getCentro().getX())
                / (dy - controlador.Controle.getPerfil2d().getCentro().getY());
        obj3d.rotacaoEixoX(angulo);


    }
    p = p1;
    desenha();
}//GEN-LAST:event_jPanelYZMouseDragged

private void jMenu2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu2ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jMenu2ActionPerformed

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
// TODO add your handling code here:
    new EditarPerspectiva(alvy).setVisible(true);
}//GEN-LAST:event_jMenuItem1ActionPerformed

private void jButtonDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteActionPerformed
    // TODO add your handling code here:
    if (jComboBoxObjetos.getItemCount() == 1) {
        jButtonLimparActionPerformed(evt);
    } else {
        Objeto3d obj3d = (Objeto3d) jComboBoxObjetos.getSelectedItem();
        Controle.getCena().remove(obj3d);
        atualiza();
    }

}//GEN-LAST:event_jButtonDeleteActionPerformed

private void jMenuItemEditarCenaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemEditarCenaActionPerformed
    // TODO add your handling code here:
    new EditarCena().setVisible(true);
}//GEN-LAST:event_jMenuItemEditarCenaActionPerformed

private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
    // TODO add your handling code here:
    new EditarObjeto((Objeto3d) jComboBoxObjetos.getSelectedItem()).setVisible(true);
}//GEN-LAST:event_jMenuItem2ActionPerformed

private void jRadioButtonComPreenchimentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonComPreenchimentoActionPerformed
// TODO add your handling code here:
    atualiza();
}//GEN-LAST:event_jRadioButtonComPreenchimentoActionPerformed

private void jRadioButtonSemPreenchimentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonSemPreenchimentoActionPerformed
// TODO add your handling code here:
    atualiza();
}//GEN-LAST:event_jRadioButtonSemPreenchimentoActionPerformed

private void jButtonRenderizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRenderizarActionPerformed
// TODO add your handling code here:
    HashSet<Objeto3d> cena = new HashSet<Objeto3d>();
    for (Objeto3d obj : Controle.getCena()) {
        Objeto3d i = new Objeto3d();
        for (Face3d f : obj.getFaces()) {
            i.addFace(new Face3d(alvy.alvyRay(f.getP1()), alvy.alvyRay(f.getP2()), alvy.alvyRay(f.getP3()), f.getCor()));
        }
        i.setCor(obj.getCor());
        cena.add(i);
    }
    new AspectoRealista(cena, alvy.getXmin(), alvy.getXmax(), alvy.getYmin(), alvy.getYmax(), new Luz(alvy.alvyRay(Controle.getLuz().getPosicaoLuz()), Controle.getLuz().getIe()), Controle.getCor()).setVisible(true);
}//GEN-LAST:event_jButtonRenderizarActionPerformed

private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
    // TODO add your handling code here:
    new EditarLuz(Controle.getLuz().getPosicaoLuz()).setVisible(true);
}//GEN-LAST:event_jMenuItem3ActionPerformed

private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
    // TODO add your handling code here:
    Controle.gravar();
}//GEN-LAST:event_jMenuItem4ActionPerformed

private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
    // TODO add your handling code here:
    Controle.ler();
    atualiza();
}//GEN-LAST:event_jMenuItem5ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TelaPrincipal().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.JButton jButtonDelete;
    private javax.swing.JButton jButtonLimpar;
    private javax.swing.JButton jButtonRenderizar;
    private javax.swing.JComboBox jComboBoxObjetos;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItemEditarCena;
    private javax.swing.JMenuItem jMenuItemNovoObjeto;
    private javax.swing.JPanel jPanelPerpectiva;
    private javax.swing.JPanel jPanelTodos;
    private javax.swing.JPanel jPanelTodosXY;
    private javax.swing.JPanel jPanelTodosXZ;
    private javax.swing.JPanel jPanelTodosYZ;
    private javax.swing.JPanel jPanelXY;
    private javax.swing.JPanel jPanelXZ;
    private javax.swing.JPanel jPanelYZ;
    private javax.swing.JRadioButton jRadioButtonComOcultacao;
    private javax.swing.JRadioButton jRadioButtonComPreenchimento;
    private javax.swing.JRadioButton jRadioButtonEscala;
    private javax.swing.JRadioButton jRadioButtonMover;
    private javax.swing.JRadioButton jRadioButtonRotacionar;
    private javax.swing.JRadioButton jRadioButtonSemOcultacao;
    private javax.swing.JRadioButton jRadioButtonSemPreenchimento;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator11;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JToolBar.Separator jSeparator7;
    private javax.swing.JToolBar.Separator jSeparator8;
    private javax.swing.JTabbedPane jTabbedPaneCena;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration//GEN-END:variables

    public void atualiza() {
        jComboBoxObjetos.removeAllItems();
        for (Objeto3d o : Controle.getCena()) {
            jComboBoxObjetos.addItem(o);
        }
        jRadioButtonComOcultacao.setEnabled(true);
        jRadioButtonEscala.setEnabled(true);
        jRadioButtonMover.setEnabled(true);
        jRadioButtonRotacionar.setEnabled(true);
        jRadioButtonSemOcultacao.setEnabled(true);
        desenha();
    }

    private void desenha() {
        try {

            int a = jTabbedPaneCena.getSelectedIndex();

            if (a == 1) {
                desenhaXY();
            }
            if (a == 2) {
                desenhaXZ();
            }
            if (a == 3) {
                desenhaYZ();
            }

            if (a == 0) {
                desenhaTodosXY();
                desenhaTodosXZ();
                desenhaTodosYZ();
                desenhaPerpectiva();
            }


        } catch (Exception e) {
        }
    }

    public boolean isOcultacao() {
        if (!jRadioButtonComOcultacao.isSelected()) {
            return true;
        }
        return false;
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        desenha();
    }

    private void desenhaXY() {
        Graphics g1 = jPanelXY.getGraphics();
        g1.setColor(Controle.getCor());
        g1.fillRect(0, 0, jPanelXY.getWidth(), jPanelXY.getHeight());
        Ponto3d observador = new Ponto3d(0, 0, 3000);
        zBuffer = new ZBuffer(observador);
        for (Objeto3d d : controlador.Controle.getCena()) {
            for (Face3d face : d.getFaces()) {
                if (isOcultacao()) {
                    zBuffer.add(face);
                } else {
                    if (face.normal(observador) < 0) {
                        zBuffer.add(face);
                    }
                }
            }
        }
        for (int i = zBuffer.getLista().size() - 1; i >= 0; i--) {
            Vetor luz = new Vetor(Controle.getLuz().getPosicaoLuz(),zBuffer.getLista().get(i).getFace().getCentro() );
            double intensidade = Vetor.produtoInterno(zBuffer.getLista().get(i).getFace().getVetor(), luz);
            int r = zBuffer.getLista().get(i).getFace().getCor().getRed();
            int g = zBuffer.getLista().get(i).getFace().getCor().getGreen();
            int b = zBuffer.getLista().get(i).getFace().getCor().getBlue();
            r = (int) (r + 255 * intensidade);
            g = (int) (g + 255 * intensidade);
            b = (int) (b + 255 * intensidade);
            if (r < 0) {
                r = zBuffer.getLista().get(i).getFace().getCor().getRed();
            }
            if (r > 255) {
                r = 255;
            }
            if (b < 0) {
                b = zBuffer.getLista().get(i).getFace().getCor().getBlue();
            }
            if (b > 255) {
                b = 255;
            }
            if (g < 0) {
                g = zBuffer.getLista().get(i).getFace().getCor().getGreen();
            }
            if (g > 255) {
                g = 255;
            }
            g1.setColor(new Color(r, g, b));

            //g1.setColor(zBuffer.getLista().get(i).getFace().getCor());
            if (jRadioButtonComPreenchimento.isSelected()) {
                int[] x = {(int) zBuffer.getLista().get(i).getFace().getP1().getX(), (int) zBuffer.getLista().get(i).getFace().getP2().getX(), (int) zBuffer.getLista().get(i).getFace().getP3().getX()};
                int[] y = {(int) zBuffer.getLista().get(i).getFace().getP1().getY(), (int) zBuffer.getLista().get(i).getFace().getP2().getY(), (int) zBuffer.getLista().get(i).getFace().getP3().getY()};
                g1.fillPolygon(x, y, 3);
                g1.setColor(Color.BLACK);
            }
            g1.drawLine(
                    (int) zBuffer.getLista().get(i).getFace().getP1().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP1().getY(),
                    (int) zBuffer.getLista().get(i).getFace().getP2().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP2().getY());
            g1.drawLine(
                    (int) zBuffer.getLista().get(i).getFace().getP1().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP1().getY(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getY());
            g1.drawLine(
                    (int) zBuffer.getLista().get(i).getFace().getP2().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP2().getY(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getY());
        }
    }

    private void desenhaPerpectiva() {
        Graphics g1 = jPanelPerpectiva.getGraphics();
        g1.setColor(Controle.getCor());
        g1.fillRect(0, 0, jPanelPerpectiva.getWidth(), jPanelPerpectiva.getHeight());
        zBuffer = new ZBuffer(alvy.getVrp());
        for (Objeto3d d : controlador.Controle.getCena()) {
            for (Face3d face : d.getFaces()) {
                if (isOcultacao()) {
                    Ponto3d p1 = alvy.alvyRay(face.getP1());
                    Ponto3d p2 = alvy.alvyRay(face.getP2());
                    Ponto3d p3 = alvy.alvyRay(face.getP3());
                    zBuffer.add(new Face3d(p1, p2, p3, d.getCor()));

                } else {
                    if (face.normal(alvy.getVrp()) < 0) {
                        Ponto3d p1 = alvy.alvyRay(face.getP1());
                        Ponto3d p2 = alvy.alvyRay(face.getP2());
                        Ponto3d p3 = alvy.alvyRay(face.getP3());
                        zBuffer.add(new Face3d(p1, p2, p3, d.getCor()));
                    }
                }
            }
        }
        for (int i = zBuffer.getLista().size() - 1; i >= 0; i--) {
            Vetor luz = new Vetor(alvy.alvyRay(zBuffer.getLista().get(i).getFace().getCentro() ),alvy.alvyRay(Controle.getLuz().getPosicaoLuz()));
            double intensidade = Vetor.produtoInterno(luz,zBuffer.getLista().get(i).getFace().getVetor() );
            int r = zBuffer.getLista().get(i).getFace().getCor().getRed();
            int g = zBuffer.getLista().get(i).getFace().getCor().getGreen();
            int b = zBuffer.getLista().get(i).getFace().getCor().getBlue();
            r = (int) (r + 255 * intensidade);
            g = (int) (g + 255 * intensidade);
            b = (int) (b + 255 * intensidade);
            if (r < 0) {
                r = zBuffer.getLista().get(i).getFace().getCor().getRed();
            }
            if (r > 255) {
                r = 255;
            }
            if (b < 0) {
                b = zBuffer.getLista().get(i).getFace().getCor().getBlue();
            }
            if (b > 255) {
                b = 255;
            }
            if (g < 0) {
                g = zBuffer.getLista().get(i).getFace().getCor().getGreen();
            }
            if (g > 255) {
                g = 255;
            }
            g1.setColor(new Color(r, g, b));
            if (jRadioButtonComPreenchimento.isSelected()) {
                int[] x = {(int) zBuffer.getLista().get(i).getFace().getP1().getX(), (int) zBuffer.getLista().get(i).getFace().getP2().getX(), (int) zBuffer.getLista().get(i).getFace().getP3().getX()};
                int[] y = {(int) zBuffer.getLista().get(i).getFace().getP1().getY(), (int) zBuffer.getLista().get(i).getFace().getP2().getY(), (int) zBuffer.getLista().get(i).getFace().getP3().getY()};
                g1.fillPolygon(x, y, 3);
                g1.setColor(Color.BLACK);
            }
            g1.drawLine(
                    (int) zBuffer.getLista().get(i).getFace().getP1().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP1().getY(),
                    (int) zBuffer.getLista().get(i).getFace().getP2().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP2().getY());
            g1.drawLine(
                    (int) zBuffer.getLista().get(i).getFace().getP1().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP1().getY(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getY());
            g1.drawLine(
                    (int) zBuffer.getLista().get(i).getFace().getP2().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP2().getY(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getY());
        }
    }

    private void desenhaXZ() {
        Graphics g2 = jPanelXZ.getGraphics();
        g2.setColor(Controle.getCor());
        g2.fillRect(0, 0, jPanelXZ.getWidth(), jPanelXZ.getHeight());
        Ponto3d observador = new Ponto3d(0, 3000, 0);
        zBuffer = new ZBuffer(observador);
        for (Objeto3d d : controlador.Controle.getCena()) {
            for (Face3d face : d.getFaces()) {
                if (isOcultacao()) {
                    zBuffer.add(face);
                } else {
                    if (face.normal(observador) < 0) {
                        zBuffer.add(face);
                    }
                }
            }
        }
        for (int i = zBuffer.getLista().size() - 1; i >= 0; i--) {
             Vetor luz = new Vetor(Controle.getLuz().getPosicaoLuz(),zBuffer.getLista().get(i).getFace().getCentro() );
            double intensidade = Vetor.produtoInterno(zBuffer.getLista().get(i).getFace().getVetor(), luz);
            int r = zBuffer.getLista().get(i).getFace().getCor().getRed();
            int g = zBuffer.getLista().get(i).getFace().getCor().getGreen();
            int b = zBuffer.getLista().get(i).getFace().getCor().getBlue();
            r = (int) (r + 255 * intensidade);
            g = (int) (g + 255 * intensidade);
            b = (int) (b + 255 * intensidade);
            if (r < 0) {
                r = zBuffer.getLista().get(i).getFace().getCor().getRed();
            }
            if (r > 255) {
                r = 255;
            }
            if (b < 0) {
                b = zBuffer.getLista().get(i).getFace().getCor().getBlue();
            }
            if (b > 255) {
                b = 255;
            }
            if (g < 0) {
                g = zBuffer.getLista().get(i).getFace().getCor().getGreen();
            }
            if (g > 255) {
                g = 255;
            }
            g2.setColor(new Color(r, g, b));
            if (jRadioButtonComPreenchimento.isSelected()) {
                int[] x = {(int) zBuffer.getLista().get(i).getFace().getP1().getX(), (int) zBuffer.getLista().get(i).getFace().getP2().getX(), (int) zBuffer.getLista().get(i).getFace().getP3().getX()};
                int[] y = {(int) zBuffer.getLista().get(i).getFace().getP1().getZ(), (int) zBuffer.getLista().get(i).getFace().getP2().getZ(), (int) zBuffer.getLista().get(i).getFace().getP3().getZ()};
                g2.fillPolygon(x, y, 3);
                g2.setColor(Color.BLACK);
            }
            g2.drawLine(
                    (int) zBuffer.getLista().get(i).getFace().getP1().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP1().getZ(),
                    (int) zBuffer.getLista().get(i).getFace().getP2().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP2().getZ());
            g2.drawLine(
                    (int) zBuffer.getLista().get(i).getFace().getP1().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP1().getZ(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getZ());
            g2.drawLine(
                    (int) zBuffer.getLista().get(i).getFace().getP2().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP2().getZ(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getZ());
        }
    }

    private void desenhaYZ() {
        Graphics g3 = jPanelYZ.getGraphics();
        g3.setColor(Controle.getCor());
        g3.fillRect(0, 0, jPanelYZ.getWidth(), jPanelYZ.getHeight());
        Ponto3d observador = new Ponto3d(3000, 0, 0);
        zBuffer = new ZBuffer(observador);
        for (Objeto3d d : controlador.Controle.getCena()) {
            for (Face3d face : d.getFaces()) {
                if (isOcultacao()) {
                    zBuffer.add(face);
                } else {
                    if (face.normal(observador) < 0) {
                        zBuffer.add(face);
                    }
                }
            }
        }
        for (int i = zBuffer.getLista().size() - 1; i >= 0; i--) {
            Vetor luz = new Vetor(Controle.getLuz().getPosicaoLuz(),zBuffer.getLista().get(i).getFace().getCentro() );
            double intensidade = Vetor.produtoInterno(zBuffer.getLista().get(i).getFace().getVetor(), luz);
            int r = zBuffer.getLista().get(i).getFace().getCor().getRed();
            int g = zBuffer.getLista().get(i).getFace().getCor().getGreen();
            int b = zBuffer.getLista().get(i).getFace().getCor().getBlue();
            r = (int) (r + 255 * intensidade);
            g = (int) (g + 255 * intensidade);
            b = (int) (b + 255 * intensidade);
            if (r < 0) {
                r = zBuffer.getLista().get(i).getFace().getCor().getRed();
            }
            if (r > 255) {
                r = 255;
            }
            if (b < 0) {
                b = zBuffer.getLista().get(i).getFace().getCor().getBlue();
            }
            if (b > 255) {
                b = 255;
            }
            if (g < 0) {
                g = zBuffer.getLista().get(i).getFace().getCor().getGreen();
            }
            if (g > 255) {
                g = 255;
            }
            g3.setColor(new Color(r, g, b));
            if (jRadioButtonComPreenchimento.isSelected()) {
                int[] x = {(int) zBuffer.getLista().get(i).getFace().getP1().getY(), (int) zBuffer.getLista().get(i).getFace().getP2().getY(), (int) zBuffer.getLista().get(i).getFace().getP3().getY()};
                int[] y = {(int) zBuffer.getLista().get(i).getFace().getP1().getZ(), (int) zBuffer.getLista().get(i).getFace().getP2().getZ(), (int) zBuffer.getLista().get(i).getFace().getP3().getZ()};
                g3.fillPolygon(x, y, 3);
                g3.setColor(Color.BLACK);
            }
            g3.drawLine(
                    (int) zBuffer.getLista().get(i).getFace().getP1().getY(),
                    (int) zBuffer.getLista().get(i).getFace().getP1().getZ(),
                    (int) zBuffer.getLista().get(i).getFace().getP2().getY(),
                    (int) zBuffer.getLista().get(i).getFace().getP2().getZ());
            g3.drawLine(
                    (int) zBuffer.getLista().get(i).getFace().getP1().getY(),
                    (int) zBuffer.getLista().get(i).getFace().getP1().getZ(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getY(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getZ());
            g3.drawLine(
                    (int) zBuffer.getLista().get(i).getFace().getP2().getY(),
                    (int) zBuffer.getLista().get(i).getFace().getP2().getZ(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getY(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getZ());
        }
    }

    private void desenhaTodosXY() {
        Graphics g4 = jPanelTodosXY.getGraphics();
        g4.setColor(Controle.getCor());
        g4.fillRect(0, 0, jPanelTodosXY.getWidth(), jPanelTodosXY.getHeight());
        Ponto3d observador = new Ponto3d(0, 0, 3000);
        zBuffer = new ZBuffer(observador);
        for (Objeto3d d : controlador.Controle.getCena()) {
            for (Face3d face : d.getFaces()) {
                if (isOcultacao()) {
                    zBuffer.add(face);
                } else {
                    if (face.normal(observador) < 0) {
                        zBuffer.add(face);
                    }
                }
            }
        }
        for (int i = zBuffer.getLista().size() - 1; i >= 0; i--) {
             Vetor luz = new Vetor(Controle.getLuz().getPosicaoLuz(),zBuffer.getLista().get(i).getFace().getCentro() );
            double intensidade = Vetor.produtoInterno(zBuffer.getLista().get(i).getFace().getVetor(), luz);
            int r = zBuffer.getLista().get(i).getFace().getCor().getRed();
            int g = zBuffer.getLista().get(i).getFace().getCor().getGreen();
            int b = zBuffer.getLista().get(i).getFace().getCor().getBlue();
            r = (int) (r + 255 * intensidade);
            g = (int) (g + 255 * intensidade);
            b = (int) (b + 255 * intensidade);
            if (r < 0) {
                r = zBuffer.getLista().get(i).getFace().getCor().getRed();
            }
            if (r > 255) {
                r = 255;
            }
            if (b < 0) {
                b = zBuffer.getLista().get(i).getFace().getCor().getBlue();
            }
            if (b > 255) {
                b = 255;
            }
            if (g < 0) {
                g = zBuffer.getLista().get(i).getFace().getCor().getGreen();
            }
            if (g > 255) {
                g = 255;
            }
            g4.setColor(new Color(r, g, b));
            if (jRadioButtonComPreenchimento.isSelected()) {
                int[] x = {(int) zBuffer.getLista().get(i).getFace().getP1().getX(), (int) zBuffer.getLista().get(i).getFace().getP2().getX(), (int) zBuffer.getLista().get(i).getFace().getP3().getX()};
                int[] y = {(int) zBuffer.getLista().get(i).getFace().getP1().getY(), (int) zBuffer.getLista().get(i).getFace().getP2().getY(), (int) zBuffer.getLista().get(i).getFace().getP3().getY()};
                g4.fillPolygon(x, y, 3);
                g4.setColor(Color.BLACK);
            }
            g4.drawLine(
                    (int) zBuffer.getLista().get(i).getFace().getP1().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP1().getY(),
                    (int) zBuffer.getLista().get(i).getFace().getP2().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP2().getY());
            g4.drawLine(
                    (int) zBuffer.getLista().get(i).getFace().getP1().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP1().getY(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getY());
            g4.drawLine(
                    (int) zBuffer.getLista().get(i).getFace().getP2().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP2().getY(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getY());
        }
    }

    private void desenhaTodosXZ() {
        Graphics g5 = jPanelTodosXZ.getGraphics();
        g5.setColor(Controle.getCor());
        g5.fillRect(0, 0, jPanelTodosXZ.getWidth(), jPanelTodosXZ.getHeight());
        Ponto3d observador = new Ponto3d(0, 3000, 0);
        zBuffer = new ZBuffer(observador);
        for (Objeto3d d : controlador.Controle.getCena()) {
            for (Face3d face : d.getFaces()) {
                if (isOcultacao()) {
                    zBuffer.add(face);
                } else {
                    if (face.normal(observador) < 0) {
                        zBuffer.add(face);
                    }
                }
            }
        }
        for (int i = zBuffer.getLista().size() - 1; i >= 0; i--) {
             Vetor luz = new Vetor(Controle.getLuz().getPosicaoLuz(),zBuffer.getLista().get(i).getFace().getCentro() );
            double intensidade = Vetor.produtoInterno(zBuffer.getLista().get(i).getFace().getVetor(), luz);
            int r = zBuffer.getLista().get(i).getFace().getCor().getRed();
            int g = zBuffer.getLista().get(i).getFace().getCor().getGreen();
            int b = zBuffer.getLista().get(i).getFace().getCor().getBlue();
            r = (int) (r + 255 * intensidade);
            g = (int) (g + 255 * intensidade);
            b = (int) (b + 255 * intensidade);
            if (r < 0) {
                r = zBuffer.getLista().get(i).getFace().getCor().getRed();
            }
            if (r > 255) {
                r = 255;
            }
            if (b < 0) {
                b = zBuffer.getLista().get(i).getFace().getCor().getBlue();
            }
            if (b > 255) {
                b = 255;
            }
            if (g < 0) {
                g = zBuffer.getLista().get(i).getFace().getCor().getGreen();
            }
            if (g > 255) {
                g = 255;
            }
            g5.setColor(new Color(r, g, b));
            if (jRadioButtonComPreenchimento.isSelected()) {
                int[] x = {(int) zBuffer.getLista().get(i).getFace().getP1().getX(), (int) zBuffer.getLista().get(i).getFace().getP2().getX(), (int) zBuffer.getLista().get(i).getFace().getP3().getX()};
                int[] y = {(int) zBuffer.getLista().get(i).getFace().getP1().getZ(), (int) zBuffer.getLista().get(i).getFace().getP2().getZ(), (int) zBuffer.getLista().get(i).getFace().getP3().getZ()};
                g5.fillPolygon(x, y, 3);
                g5.setColor(Color.BLACK);
            }
            g5.drawLine(
                    (int) zBuffer.getLista().get(i).getFace().getP1().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP1().getZ(),
                    (int) zBuffer.getLista().get(i).getFace().getP2().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP2().getZ());
            g5.drawLine(
                    (int) zBuffer.getLista().get(i).getFace().getP1().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP1().getZ(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getZ());
            g5.drawLine(
                    (int) zBuffer.getLista().get(i).getFace().getP2().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP2().getZ(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getX(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getZ());
        }
    }

    private void desenhaTodosYZ() {
        Graphics g6 = jPanelTodosYZ.getGraphics();
        g6.setColor(Controle.getCor());
        g6.fillRect(0, 0, jPanelTodosYZ.getWidth(), jPanelTodosYZ.getHeight());
        Ponto3d observador = new Ponto3d(3000, 0, 0);
        zBuffer = new ZBuffer(observador);
        for (Objeto3d d : controlador.Controle.getCena()) {
            for (Face3d face : d.getFaces()) {
                if (isOcultacao()) {
                    zBuffer.add(face);
                } else {
                    if (face.normal(observador) < 0) {
                        zBuffer.add(face);
                    }
                }
            }
        }
        for (int i = zBuffer.getLista().size() - 1; i >= 0; i--) {
             Vetor luz = new Vetor(Controle.getLuz().getPosicaoLuz(),zBuffer.getLista().get(i).getFace().getCentro() );
            double intensidade = Vetor.produtoInterno(zBuffer.getLista().get(i).getFace().getVetor(), luz);
            int r = zBuffer.getLista().get(i).getFace().getCor().getRed();
            int g = zBuffer.getLista().get(i).getFace().getCor().getGreen();
            int b = zBuffer.getLista().get(i).getFace().getCor().getBlue();
            r = (int) (r + 255 * intensidade);
            g = (int) (g + 255 * intensidade);
            b = (int) (b + 255 * intensidade);
            if (r < 0) {
                r = zBuffer.getLista().get(i).getFace().getCor().getRed();
            }
            if (r > 255) {
                r = 255;
            }
            if (b < 0) {
                b = zBuffer.getLista().get(i).getFace().getCor().getBlue();
            }
            if (b > 255) {
                b = 255;
            }
            if (g < 0) {
                g = zBuffer.getLista().get(i).getFace().getCor().getGreen();
            }
            if (g > 255) {
                g = 255;
            }
            g6.setColor(new Color(r, g, b));
            if (jRadioButtonComPreenchimento.isSelected()) {
                int[] x = {(int) zBuffer.getLista().get(i).getFace().getP1().getY(), (int) zBuffer.getLista().get(i).getFace().getP2().getY(), (int) zBuffer.getLista().get(i).getFace().getP3().getY()};
                int[] y = {(int) zBuffer.getLista().get(i).getFace().getP1().getZ(), (int) zBuffer.getLista().get(i).getFace().getP2().getZ(), (int) zBuffer.getLista().get(i).getFace().getP3().getZ()};
                g6.fillPolygon(x, y, 3);
                g6.setColor(Color.BLACK);
            }
            g6.drawLine(
                    (int) zBuffer.getLista().get(i).getFace().getP1().getY(),
                    (int) zBuffer.getLista().get(i).getFace().getP1().getZ(),
                    (int) zBuffer.getLista().get(i).getFace().getP2().getY(),
                    (int) zBuffer.getLista().get(i).getFace().getP2().getZ());
            g6.drawLine(
                    (int) zBuffer.getLista().get(i).getFace().getP1().getY(),
                    (int) zBuffer.getLista().get(i).getFace().getP1().getZ(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getY(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getZ());
            g6.drawLine(
                    (int) zBuffer.getLista().get(i).getFace().getP2().getY(),
                    (int) zBuffer.getLista().get(i).getFace().getP2().getZ(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getY(),
                    (int) zBuffer.getLista().get(i).getFace().getP3().getZ());
        }
    }
    private AlvyRay alvy =
            new AlvyRay(
            new Ponto3d(500, 500, 500),
            8,
            new Ponto3d(100, 100, 0),
            0,
            0,
            400,
            300,
            70,
            130,
            4,
            3,
            0,
            1,
            0);

    public void setAlvy(double VRPx, double VRPy, double VRPz,
            double d,
            double Px, double Py, double Pz, //pontofocal
            double xmin,
            double ymin,
            double xmax,
            double ymax,
            double n,
            double f,
            double su,
            double sv,
            double Yx,
            double Yy,
            double Yz) {


        alvy = new AlvyRay(new Ponto3d(VRPx, VRPy, VRPz),
                d,
                new Ponto3d(Px, Py, Pz),
                xmin,
                ymin,
                xmax,
                ymax,
                n,
                f,
                su,
                sv,
                Yx,
                Yy,
                Yz);

        desenha();
    }

    public AlvyRay getAlvyRay() {
        return alvy;
    }
}
